package devices

import (
	"context"
	v1 "devops-super/api/product/v1"
	"devops-super/internal/dao"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
)

func (s *sDevice) GetStateLogs(ctx context.Context, req *v1.DeviceGetStateLogsReq) (res *v1.DeviceGetStateLogsRes, err error) {
	res = new(v1.DeviceGetStateLogsRes)
	var data = make([]*entity.IotDeviceStateLog, 0)
	m := dao.IotDeviceStateLog.Ctx(ctx).
		Where(dao.IotDeviceStateLog.Columns().DeviceNos+" like ? ", "%"+req.DeviceNo+"%")
	isSuperDept, currentUser, _ := service.User().IsCurrentUserSuperDept(ctx)
	if !isSuperDept {
		m = m.Where(dao.IotDeviceStateLog.Columns().DeptId, currentUser.DeptId)
	}
	err = m.OrderDesc("id").Limit(500).Scan(&data)
	if err != nil {
		return nil, err
	}
	res.List = data
	return
}
